﻿Ext.define("PSI.OutPlan.EvaluateService", {
    setQC: function (qc) {
        this.qc = qc;
    },

    createToolButtons: function () {
        var me = this;
        var p = me.permission;
        var result = [];
        result.push("-");
        result.push({ text: "服务评价", iconCls: "psi-button-comments", handler: me.onComments, scope: me });
        result.push("-");
        result.push({ text: "刷新", iconCls: "psi-button-refresh", handler: me.onRefreshPlanGird, scope: me });
        //result.push("-");
        //result.push({ text: "导出Excel", iconCls: "psi-button-excel", handler: me.onExcel, scope: me });

        result.push("-");
        result.push({
            text: "关闭", iconCls: "psi-button-exit", handler: function () {
                if (me.qc && (me.qc.billNumber || me.qc.planNumber)) {
                    if (Ext.isChrome) {
                        window.open("", "_self", "");
                        window.close();
                    } else if (Ext.isIE) {
                        window.close();
                    } else {
                        location.replace("/");
                    }
                } else {
                    location.replace("/");
                }
            }
        });
        return result;
    },

    createUI: function () {
        var me = this;

        var panel = Ext.create("Ext.panel.Panel", {
            border: 0,
            layout: "border",
            title: "出口计划服务评价",
            tbar: me.createToolButtons(),
            items: [{
                region: "north", xtype: "container",
                height: 90,
                layout: {
                    type: "table",
                    columns: 8
                }, border: false,
                items: [{
                    xtype: "label",
                    text: "计划号"
                }, {
                    xtype: "textfield",
                    id: "editPlanNumberQC",
                    margin: "4 5 0 5",
                    width: 170,
                    value: me.qc ? me.qc.planNumber : null
                }, {
                    xtype: "label",
                    text: "门点"
                }, {
                    xtype: "psi_bc_addressfield",
                    margin: "4 5 0 5",
                    id: "editAddressQC",
                    width: 170
                }, {
                    xtype: "label",
                    text: "箱号"
                }, {
                    xtype: "textfield",
                    id: "editCntNumberQC",
                    margin: "4 5 0 5",
                    width: 170
                }, {
                    xtype: "label",
                    text: "调度"
                }, {
                    xtype: "psi_bc_dispatcherfield",
                    margin: "4 5 0 5",
                    id: "editDispatcherQC",
                    width: 170
                }, {
                    xtype: "label",
                    text: "拖车公司"
                }, {
                    xtype: "psi_truck_truckcompanyfield",
                    margin: "4 5 0 5",
                    id: "editTruckCompanyQC",
                    width: 170
                }, {
                    xtype: "label",
                    text: "车号"
                }, {
                    xtype: "psi_truck_trucknumberfield",
                    id: "editTruckNumberQC",
                    margin: "4 5 0 5",
                    width: 170
                }, {
                    xtype: "label",
                    text: "司机"
                }, {
                    xtype: "textfield",
                    id: "editDriverQC",
                    margin: "4 5 0 5",
                    width: 170
                }, {
                    xtype: "label",
                    text: "司机电话"
                }, {
                    xtype: "textfield",
                    id: "editDriverTelQC",
                    margin: "4 5 0 5",
                    width: 170
                }, {
                    xtype: "label",
                    text: "计划下达日"
                }, {
                    xtype: "datefield",
                    margin: "4 5 0 5",
                    format: "Y-m-d",
                    width: 170,
                    id: "editPlanDT1QC"
                }, {
                    xtype: "label",
                    text: "----"
                }, {
                    xtype: "datefield",
                    margin: "4 5 0 5",
                    format: "Y-m-d",
                    width: 170,
                    id: "editPlanDT2QC"
                }, {
                    xtype: "label",
                    text: "委托单号"
                }, {
                    xtype: "textfield",
                    id: "editConsignationBillNumberQC",
                    margin: "4 5 0 5",
                    width: 170,
                    value: me.qc ? me.qc.billNumber : null
                }, {
                    xtype: "container",
                    border: 0,
                    layout: "hbox",
                    items: [
                        {
                            xtype: "button",
                            text: "查询",
                            iconCls: "psi-button-refresh",
                            margin: "4 5 0 5",
                            width: 90,
                            handler: me.onRefreshPlanGird,
                            scope: me
                        }, {
                            xtype: "button",
                            text: "清空查询条件",
                            margin: "4 5 0 5",
                            handler: me.onClearQC,
                            scope: me
                        }
                    ],
                    colspan: 2
                }
                ]
            }, {
                region: "center", xtype: "container", layout: "fit", border: false,
                items: [me.getPlanGrid()]
            }, {
                region: "south", xtype: "container", border: false, height: 30,
                items: [
                    {
                        xtype: "displayfield",
                        id: "editPlanInfo",
                        value: ""
                    }
                ]
            }
            ]
        });

        me.panel = panel;

        Ext.getCmp("editTruckNumberQC").setLinkedEditor({
            truckCompanyEditor: Ext.getCmp("editTruckCompanyQC")
        });

        if (me.qc && (me.qc.billNumber || me.qc.planNumber)) {
            me.refreshPlanGrid();
        }
    },

    getMainPanel: function () {
        return this.panel;
    },

    getPlanGrid: function () {
        var me = this;

        if (!me.__PlanGrid) {
            me.__PlanGrid = me.createPlanGrid();
        }

        return me.__PlanGrid;
    },

    createPlanGrid: function () {
        var modelName = "PSIOutPlanModel";
        Ext.define(modelName, {
            extend: "Ext.data.Model",
            fields: ["id", "planNumber", "address", "cntNumber", "cntType", "takeCntDT", "memo",
                            "planDT", "dispatcher", "truckDT", "truckCompany", "truckNumber",
                            "driver", "driverTel", "dt0Plan", "dt0Driver", "dt1Plan", "dt1Driver", "dt2Plan", "dt2Driver",
                            "dt3Plan", "dt3Driver", "dt4Plan", "dt4Driver", "vsl", "clients", "cntOwner", "status", "srvLevel",
                            "loadCntMan", "loadCntManTel", "traceMemo", "ton", "destPort", "goods"
            ]
        });

        return Ext.create("Ext.grid.Panel", {
            viewConfig: { enableTextSelection: true },
            columnLines: true,
            columns: [
                    Ext.create("Ext.grid.RowNumberer", { text: "", width: 30 }),
                    { header: "状态", dataIndex: "status", menuDisabled: true },
                    { header: "服务评价", dataIndex: "srvLevel", menuDisabled: true },
                    { header: "出口计划号", dataIndex: "planNumber", menuDisabled: true },
                    { header: "船名/航次", dataIndex: "vsl", menuDisabled: true },
                    { header: "委托方", dataIndex: "clients", menuDisabled: true },
                    { header: "门点", dataIndex: "address", menuDisabled: true },
                    { header: "箱属", dataIndex: "cntOwner", menuDisabled: true },
                    { header: "箱号/封号", dataIndex: "cntNumber", menuDisabled: true, width: 230 },
                    { header: "箱型", dataIndex: "cntType", menuDisabled: true, width: 60 },
                    { header: "货物", dataIndex: "goods", menuDisabled: true },
                    { header: "吨位", dataIndex: "ton", menuDisabled: true },
                    { header: "目的港", dataIndex: "destPort", menuDisabled: true },
                    { header: "装箱时限", dataIndex: "takeCntDT", menuDisabled: true, width: 120 },
                    { header: "备注", dataIndex: "memo", menuDisabled: true },
                    { header: "计划下达时间", dataIndex: "planDT", menuDisabled: true, width: 120 },
                    { header: "调度", dataIndex: "dispatcher", menuDisabled: true },
                    { header: "拖车公司", dataIndex: "truckCompany", menuDisabled: true },
                    { header: "车号", dataIndex: "truckNumber", menuDisabled: true },
                    { header: "司机", dataIndex: "driver", menuDisabled: true },
                    { header: "司机电话", dataIndex: "driverTel", menuDisabled: true },
                    { header: "派车时间", dataIndex: "truckDT", menuDisabled: true, width: 120 },
                    { header: "装箱联系人", dataIndex: "loadCntMan", menuDisabled: true },
                    { header: "装箱人电话", dataIndex: "loadCntManTel", menuDisabled: true },
                      {
                          header: "到装货地", menuDisabled: true,
                          columns: [
                              { header: "调度计划时间", dataIndex: "dt0Plan", menuDisabled: true, width: 120 },
                              { header: "司机报点", dataIndex: "dt0Driver", menuDisabled: true, width: 120 }
                          ]
                      },
                    {
                        header: "装箱开始", menuDisabled: true,
                        columns: [
                            { header: "调度计划时间", dataIndex: "dt1Plan", menuDisabled: true, width: 120 },
                            { header: "司机报点", dataIndex: "dt1Driver", menuDisabled: true, width: 120 }
                        ]
                    },
                    {
                        header: "装货完成", menuDisabled: true,
                        columns: [
                            { header: "调度计划时间", dataIndex: "dt2Plan", menuDisabled: true, width: 120 },
                            { header: "司机报点", dataIndex: "dt2Driver", menuDisabled: true, width: 120 }
                        ]
                    },
                    {
                        header: "回港", menuDisabled: true,
                        columns: [
                            { header: "调度计划时间", dataIndex: "dt3Plan", menuDisabled: true, width: 120 },
                            { header: "司机报点", dataIndex: "dt3Driver", menuDisabled: true, width: 120 }
                        ]
                    }, {
                        header: "完成", menuDisabled: true,
                        columns: [
                            { header: "调度计划时间", dataIndex: "dt4Plan", menuDisabled: true, width: 120 },
                            { header: "司机报点", dataIndex: "dt4Driver", menuDisabled: true, width: 120 }
                        ]
                    }, {
                        header: "动态备注", dataIndex: "traceMemo", menuDisabled: true
                    }
            ],
            store: Ext.create("Ext.data.Store", {
                autoLoad: false,
                model: modelName,
                data: []
            })
        });
    },

    refreshPlanGrid: function (selectedId) {
        var me = this;
        var grid = me.getPlanGrid();
        var store = grid.getStore();
        var items = grid.getSelectionModel().getSelection();
        if (selectedId == null && items && items.length > 0) {
            selectedId = items[0].getData().id;
        }
        var params = {
            planNumber: Ext.getCmp("editPlanNumberQC").getValue(),
            address: Ext.getCmp("editAddressQC").getValue(),
            cntNumber: Ext.getCmp("editCntNumberQC").getValue(),
            dispatcher: Ext.getCmp("editDispatcherQC").getValue(),
            truckCompany: Ext.getCmp("editTruckCompanyQC").getValue(),
            truckNumber: Ext.getCmp("editTruckNumberQC").getValue(),
            driver: Ext.getCmp("editDriverQC").getValue(),
            driverTel: Ext.getCmp("editDriverTelQC").getValue(),
            planDT1: Ext.getCmp("editPlanDT1QC").getValue(),
            planDT2: Ext.getCmp("editPlanDT2QC").getValue(),
            status: "运输完成",
            consignationBillNumber: Ext.getCmp("editConsignationBillNumberQC").getValue()
        };
        var el = grid.getEl() || Ext.getBody();
        el.mask("数据加载中...");
        Ext.Ajax.request({
            url: "/OutPlan/GetOutPlanListData",
            params: params,
            method: "POST",
            callback: function (options, success, response) {
                store.removeAll();

                if (success) {
                    var data = Ext.JSON.decode(response.responseText);
                    store.add(data);

                    if (selectedId) {
                        for (var i = 0; i < store.getCount() ; i++) {
                            var m = store.getAt(i);
                            if (m.get("id") == selectedId) {
                                grid.getSelectionModel().select(m);
                                break;
                            }
                        }
                    } else {
                        if (data.length > 0) {
                            grid.getSelectionModel().select(0);
                        }
                    }
                    var info = "出口计划共计: <span style='color:red'>" + data.length + "</span>个";
                    Ext.getCmp("editPlanInfo").setValue(info);
                } else {
                    PSI.MsgBox.showInfo("网络错误");
                }

                el.unmask();
            }
        });
    },

    onRefreshPlanGird: function () {
        this.refreshPlanGrid();
    },

    onExcel: function () {
        var p = {
            planNumber: Ext.getCmp("editPlanNumberQC").getValue(),
            address: Ext.getCmp("editAddressQC").getValue(),
            cntNumber: Ext.getCmp("editCntNumberQC").getValue(),
            dispatcher: Ext.getCmp("editDispatcherQC").getValue(),
            truckCompany: Ext.getCmp("editTruckCompanyQC").getValue(),
            truckNumber: Ext.getCmp("editTruckNumberQC").getValue(),
            driver: Ext.getCmp("editDriverQC").getValue(),
            driverTel: Ext.getCmp("editDriverTelQC").getValue(),
            planDT1: Ext.getCmp("editPlanDT1QC").getValue(),
            planDT2: Ext.getCmp("editPlanDT2QC").getValue(),
            consignationBillNumber: Ext.getCmp("editConsignationBillNumberQC").getValue()
        };

        var url = "/OutPlan/Excel?planNumber=" + p.planNumber + "&address=" + p.address
            + "&cntNumber=" + p.cntNumber + "&dispatcher=" + p.dispatcher
            + "&truckCompnay=" + p.truckCompany
            + "&truckNumber=" + p.truckNumber + "&driver=" + p.driver
            + "&driverTel=" + p.driverTel;
        if (p.planDT1) {
            url += "&planDT1=" + Ext.Date.format(p.planDT1, "Y-m-d");
        }
        if (p.planDT2) {
            url += "&planDT2=" + Ext.Date.format(p.planDT2, "Y-m-d");
        }
        url += "&consignationBillNumber=" + p.consignationBillNumber;

        window.open(url);
    },


    onClearQC: function () {
        Ext.getCmp("editPlanNumberQC").setValue(null);
        Ext.getCmp("editAddressQC").setValue(null);
        Ext.getCmp("editCntNumberQC").setValue(null);
        Ext.getCmp("editDispatcherQC").setValue(null);
        Ext.getCmp("editTruckCompanyQC").setValue(null);
        Ext.getCmp("editTruckNumberQC").setValue(null);
        Ext.getCmp("editDriverQC").setValue(null);
        Ext.getCmp("editDriverTelQC").setValue(null);
        Ext.getCmp("editPlanDT1QC").setValue(null);
        Ext.getCmp("editPlanDT2QC").setValue(null);
        Ext.getCmp("editConsignationBillNumberQC").setValue(null);
    },

    getStatusSelect: function () {
        var states = Ext.create('Ext.data.Store', {
            fields: ['id', 'name'],
            data: [
                { "id": "0", "name": "  " },
                { "id": "1", "name": "待下达计划" },
                { "id": "2", "name": "带派车" },
                { "id": "3", "name": "已派车" },
                { "id": "4", "name": "运输完成" },
            ]
        });

        return Ext.create('Ext.form.ComboBox', {
            id: "editStatus",
            name: "editStatus",
            margin: "4 5 0 5",
            store: states,
            width: 170,
            displayField: 'name',
            valueField: 'name'
        })
    },

    onComments: function () {
        var me = this;
        var items = me.getPlanGrid().getSelectionModel().getSelection();
        if (items == null || items.length != 1) {
            PSI.MsgBox.showInfo("请选择一个出口计划");
            return;
        }
        var plan = items[0].getData();
        var form = Ext.create("PSI.OutPlan.TraceComments");
        form.show(me, plan);
    }
});